#ifndef __U2WorkQueue_H__
#define __U2WorkQueue_H__


#include "U2PreRequest.h"
#include "U2MemoryAllocatorConfig.h"
#include "U2SharedPtr.h"
#include "U2AtomicWrapper.h"
#include "U2Any.h"



U2EG_NAMESPACE_BEGIN


/** Interface to a general purpose request / response style background work queue.
@remarks
	A work queue is a simple structure, where requests for work are placed
	onto the queue, then removed by a worker for processing, then finally
	a response is placed on the result queue for the originator to pick up
	at their leisure. The typical use for this is in a threaded environment, 
	although any kind of deferred processing could use this approach to 
	decouple and distribute work over a period of time even 
	if it was single threaded.
@par
	WorkQueues also incorporate thread pools. One or more background worker threads
	can wait on the queue and be notified when a request is waiting to be
	processed. For maximal thread usage, a WorkQueue instance should be shared
	among many sources of work, rather than many work queues being created.
	This way, you can share a small number of hardware threads among a large 
	number of background tasks. This doesn't mean you have to implement all the
	request processing in one class, you can plug in many handlers in order to
	process the requests.
@par
	This is an abstract interface definition; users can subclass this and 
	provide their own implementation if required to centralise task management
	in their own subsystems. We also provide a default implementation in the
	form of DefaultWorkQueue.
*/
class _U2Share WorkQueue : public UtilityAlloc
{
protected:
	typedef std::map<U2String, u2uint16> ChannelMap;
	ChannelMap mChannelMap;
	u2uint16 mNextChannel;
	U2_MUTEX(mChannelMapMutex)

public:
	/// Numeric identifier for a request
	typedef unsigned long long int RequestID;

	/** General purpose request structure. 
	*/
	class _U2Share Request : public UtilityAlloc
	{
		friend class WorkQueue;
	protected:
		/// The request channel, as an integer 
		u2uint16 mChannel;
		/// The request type, as an integer within the channel (user can define enumerations on this)
		u2uint16 mType;
		/// The details of the request (user defined)
		Any mData;
		/// Retry count - set this to non-zero to have the request try again on failure
		u2uint8 mRetryCount;
		/// Identifier (assigned by the system)
		RequestID mID;
		/// Abort Flag
		mutable bool mAborted;

	public:
		/// Constructor 
		Request(u2uint16 channel, u2uint16 rtype, const Any& rData, u2uint8 retry, RequestID rid);
		~Request();
		/// Set the abort flag
		void abortRequest() const { mAborted = true; }
		/// Get the request channel (top level categorisation)
		u2uint16 getChannel() const { return mChannel; }
		/// Get the type of this request within the given channel
		u2uint16 getType() const { return mType; }
		/// Get the user details of this request
		const Any& getData() const { return mData; }
		/// Get the remaining retry count
		u2uint8 getRetryCount() const { return mRetryCount; }
		/// Get the identifier of this request
		RequestID getID() const { return mID; }
		/// Get the abort flag
		bool getAborted() const { return mAborted; }
	};

	/** General purpose response structure. 
	*/
	struct _U2Share Response : public UtilityAlloc
	{
		/// Pointer to the request that this response is in relation to
		const Request* mRequest;
		/// Whether the work item succeeded or not
		bool mSuccess;
		/// Any diagnostic messages
		U2String mMessages;
		/// Data associated with the result of the process
		Any mData;

	public:
		Response(const Request* rq, bool success, const Any& data, const U2String& msg = U2StringUtil::BLANK);
		~Response();
		/// Get the request that this is a response to (NB destruction destroys this)
		const Request* getRequest() const { return mRequest; }
		/// Return whether this is a successful response
		bool succeeded() const { return mSuccess; }
		/// Get any diagnostic messages about the process
		const U2String& getMessages() const { return mMessages; }
		/// Return the response data (user defined, only valid on success)
		const Any& getData() const { return mData; }
		/// Abort the request
		void abortRequest() { mRequest->abortRequest(); mData.destroy(); }
	};

	/** Interface definition for a handler of requests. 
	@remarks
	User classes are expected to implement this interface in order to
	process requests on the queue. It's important to realise that
	the calls to this class may be in a separate thread to the main
	render context, and as such it may not be possible to make
	rendersystem or other GPU-dependent calls in this handler. You can only
	do so if the queue was created with 'workersCanAccessRenderSystem'
	set to true, and U2_THREAD_SUPPORT=1, but this puts extra strain
	on the thread safety of the render system and is not recommended.
	It is best to perform CPU-side work in these handlers and let the
	response handler transfer results to the GPU in the main render thread.
	*/
	class _U2Share RequestHandler
	{
	public:
		RequestHandler() {}
		virtual ~RequestHandler() {}

		/** Return whether this handler can process a given request. 
		@remarks
		Defaults to true, but if you wish to add several handlers each of
		which deal with different types of request, you can override
		this method. 
		*/
		virtual bool canHandleRequest(const Request* req, const WorkQueue* srcQ) 
		{ (void)srcQ; return !req->getAborted(); }

		/** The handler method every subclass must implement. 
		If a failure is encountered, return a Response with a failure
		result rather than raise an exception.
		@param req The Request structure, which is effectively owned by the
		handler during this call. It must be attached to the returned
		Response regardless of success or failure.
		@param srcQ The work queue that this request originated from
		@return Pointer to a Response object - the caller is responsible
		for deleting the object.
		*/
		virtual Response* handleRequest(const Request* req, const WorkQueue* srcQ) = 0;
	};

	/** Interface definition for a handler of responses. 
	@remarks
	User classes are expected to implement this interface in order to
	process responses from the queue. All calls to this class will be 
	in the main render thread and thus all GPU resources will be
	available. 
	*/
	class _U2Share ResponseHandler
	{
	public:
		ResponseHandler() {}
		virtual ~ResponseHandler() {}

		/** Return whether this handler can process a given response. 
		@remarks
		Defaults to true, but if you wish to add several handlers each of
		which deal with different types of response, you can override
		this method. 
		*/
		virtual bool canHandleResponse(const Response* res, const WorkQueue* srcQ) 
		{ (void)srcQ; return !res->getRequest()->getAborted(); }

		/** The handler method every subclass must implement. 
		@param res The Response structure. The caller is responsible for
		deleting this after the call is made, none of the data contained
		(except pointers to structures in user Any data) will persist
		after this call is returned.
		@param srcQ The work queue that this request originated from
		*/
		virtual void handleResponse(const Response* res, const WorkQueue* srcQ) = 0;
	};

	WorkQueue() : mNextChannel(0) {}
	virtual ~WorkQueue() {}

	/** Start up the queue with the options that have been set.
	@param forceRestart If the queue is already running, whether to shut it
		down and restart.
	*/
	virtual void startup(bool forceRestart = true) = 0;
	/** Add a request handler instance to the queue. 
	@remarks
		Every queue must have at least one request handler instance for each 
		channel in which requests are raised. If you 
		add more than one handler per channel, then you must implement canHandleRequest 
		differently	in each if you wish them to respond to different requests.
	@param channel The channel for requests you want to handle
	@param rh Your handler
	*/
	virtual void addRequestHandler(u2uint16 channel, RequestHandler* rh) = 0;
	/** Remove a request handler. */
	virtual void removeRequestHandler(u2uint16 channel, RequestHandler* rh) = 0;

	/** Add a response handler instance to the queue. 
	@remarks
		Every queue must have at least one response handler instance for each 
		channel in which requests are raised. If you add more than one, then you 
		must implement canHandleResponse differently in each if you wish them 
		to respond to different responses.
	@param channel The channel for responses you want to handle
	@param rh Your handler
	*/
	virtual void addResponseHandler(u2uint16 channel, ResponseHandler* rh) = 0;
	/** Remove a Response handler. */
	virtual void removeResponseHandler(u2uint16 channel, ResponseHandler* rh) = 0;

	/** Add a new request to the queue.
	@param channel The channel this request will go into = 0; the channel is the top-level
		categorisation of the request
	@param requestType An identifier that's unique within this queue which
		identifies the type of the request (user decides the actual value)
	@param rData The data required by the request process. 
	@param retryCount The number of times the request should be retried
		if it fails.
	@param forceSynchronous Forces the request to be processed immediately
		even if threading is enabled.
	@returns The ID of the request that has been added
	*/
	virtual RequestID addRequest(u2uint16 channel, u2uint16 requestType, const Any& rData, u2uint8 retryCount = 0, 
		bool forceSynchronous = false) = 0;

	/** Abort a previously issued request.
	If the request is still waiting to be processed, it will be 
	removed from the queue.
	@param id The ID of the previously issued request.
	*/
	virtual void abortRequest(RequestID id) = 0;

	/** Abort all previously issued requests in a given channel.
	Any requests still waiting to be processed of the given channel, will be 
	removed from the queue.
	@param channel The type of request to be aborted
	*/
	virtual void abortRequestsByChannel(u2uint16 channel) = 0;

	/** Abort all previously issued requests.
	Any requests still waiting to be processed will be removed from the queue.
	Any requests that are being processed will still complete.
	*/
	virtual void abortAllRequests() = 0;
	
	/** Set whether to pause further processing of any requests. 
	If true, any further requests will simply be queued and not processed until
	setPaused(false) is called. Any requests which are in the process of being
	worked on already will still continue. 
	*/
	virtual void setPaused(bool pause) = 0;
	/// Return whether the queue is paused ie not sending more work to workers
	virtual bool isPaused() const = 0;

	/** Set whether to accept new requests or not. 
	If true, requests are added to the queue as usual. If false, requests
	are silently ignored until setRequestsAccepted(true) is called. 
	*/
	virtual void setRequestsAccepted(bool accept) = 0;
	/// Returns whether requests are being accepted right now
	virtual bool getRequestsAccepted() const = 0;

	/** Process the responses in the queue.
	@remarks
		This method is public, and must be called from the main render
		thread to 'pump' responses through the system. The method will usually
		try to clear all responses before returning = 0; however, you can specify
		a time limit on the response processing to limit the impact of
		spikes in demand by calling setResponseProcessingTimeLimit.
	*/
	virtual void processResponses() = 0; 

	/** Get the time limit imposed on the processing of responses in a
		single frame, in milliseconds (0 indicates no limit).
	*/
	virtual unsigned long getResponseProcessingTimeLimit() const = 0;

	/** Set the time limit imposed on the processing of responses in a
		single frame, in milliseconds (0 indicates no limit).
		This sets the maximum time that will be spent in processResponses() in 
		a single frame. The default is 8ms.
	*/
	virtual void setResponseProcessingTimeLimit(unsigned long ms) = 0;

	/** Shut down the queue.
	*/
	virtual void shutdown() = 0;

	/** Get a channel ID for a given channel name. 
	@remarks
		Channels are assigned on a first-come, first-served basis and are
		not persistent across application instances. This method allows 
		applications to not worry about channel clashes through manually
		assigned channel numbers.
	*/
	virtual u2uint16 getChannel(const U2String& channelName);

};

/** Base for a general purpose request / response style background work queue.
*/
class _U2Share U2DefaultWorkQueueBase : public WorkQueue
{
public:

	/** Constructor.
		Call startup() to initialise.
	@param name Optional name, just helps to identify logging output
	*/
	U2DefaultWorkQueueBase(const U2String& name = U2StringUtil::BLANK);
	virtual ~U2DefaultWorkQueueBase();
	/// Get the name of the work queue
	const U2String& getName() const;
	/** Get the number of worker threads that this queue will start when 
		startup() is called. 
	*/
	virtual size_t getWorkerThreadCount() const;

	/** Set the number of worker threads that this queue will start
		when startup() is called (default 1).
		Calling this will have no effect unless the queue is shut down and
		restarted.
	*/
	virtual void setWorkerThreadCount(size_t c);

	/** Get whether worker threads will be allowed to access render system
		resources. 
		Accessing render system resources from a separate thread can require that
		a context is maintained for that thread. Also, it requires that the
		render system is running in threadsafe mode, which only happens
		when U2_THREAD_SUPPORT=1. This option defaults to false, which means
		that threads can not use GPU resources, and the render system can 
		work in non-threadsafe mode, which is more efficient.
	*/
	virtual bool getWorkersCanAccessRenderSystem() const;


	/** Set whether worker threads will be allowed to access render system
		resources. 
		Accessing render system resources from a separate thread can require that
		a context is maintained for that thread. Also, it requires that the
		render system is running in threadsafe mode, which only happens
		when U2_THREAD_SUPPORT=1. This option defaults to false, which means
		that threads can not use GPU resources, and the render system can 
		work in non-threadsafe mode, which is more efficient.
		Calling this will have no effect unless the queue is shut down and
		restarted.
	*/
	virtual void setWorkersCanAccessRenderSystem(bool access);

	/** Process the next request on the queue. 
	@remarks
		This method is public, but only intended for advanced users to call. 
		The only reason you would call this, is if you were using your 
		own thread to drive the worker processing. The thread calling this
		method will be the thread used to call the RequestHandler.
	*/
	virtual void _processNextRequest();

	/// Main function for each thread spawned.
	virtual void _threadMain() = 0;

	/** Returns whether the queue is trying to shut down. */
	virtual bool isShuttingDown() const { return mShuttingDown; }

	/// @copydoc WorkQueue::addRequestHandler
	virtual void addRequestHandler(u2uint16 channel, RequestHandler* rh);
	/// @copydoc WorkQueue::removeRequestHandler
	virtual void removeRequestHandler(u2uint16 channel, RequestHandler* rh);
	/// @copydoc WorkQueue::addResponseHandler
	virtual void addResponseHandler(u2uint16 channel, ResponseHandler* rh);
	/// @copydoc WorkQueue::removeResponseHandler
	virtual void removeResponseHandler(u2uint16 channel, ResponseHandler* rh);

	/// @copydoc WorkQueue::addRequest
	virtual RequestID addRequest(u2uint16 channel, u2uint16 requestType, const Any& rData, u2uint8 retryCount = 0, 
		bool forceSynchronous = false);
	/// @copydoc WorkQueue::abortRequest
	virtual void abortRequest(RequestID id);
	/// @copydoc WorkQueue::abortRequestsByChannel
	virtual void abortRequestsByChannel(u2uint16 channel);
	/// @copydoc WorkQueue::abortAllRequests
	virtual void abortAllRequests();
	/// @copydoc WorkQueue::setPaused
	virtual void setPaused(bool pause);
	/// @copydoc WorkQueue::isPaused
	virtual bool isPaused() const;
	/// @copydoc WorkQueue::setRequestsAccepted
	virtual void setRequestsAccepted(bool accept);
	/// @copydoc WorkQueue::getRequestsAccepted
	virtual bool getRequestsAccepted() const;
	/// @copydoc WorkQueue::processResponses
	virtual void processResponses(); 
	/// @copydoc WorkQueue::getResponseProcessingTimeLimit
	virtual unsigned long getResponseProcessingTimeLimit() const { return mResposeTimeLimitMS; }
	/// @copydoc WorkQueue::setResponseProcessingTimeLimit
	virtual void setResponseProcessingTimeLimit(unsigned long ms) { mResposeTimeLimitMS = ms; }

protected:
	U2String mName;
	size_t mWorkerThreadCount;
	bool mWorkerRenderSystemAccess;
	bool mIsRunning;
	unsigned long mResposeTimeLimitMS;

    typedef std::deque<Request*> RequestQueue;
	typedef std::deque<Response*> ResponseQueue;
	RequestQueue mRequestQueue;
	RequestQueue mProcessQueue;
	ResponseQueue mResponseQueue;

	/// Thread function
	struct WorkerFunc U2_THREAD_WORKER_INHERIT
	{
		U2DefaultWorkQueueBase* mQueue;

		WorkerFunc(U2DefaultWorkQueueBase* q) 
			: mQueue(q) {}

		void operator()();

		void run();
	};
	WorkerFunc* mWorkerFunc;

	/** Intermediate structure to hold a pointer to a request handler which 
		provides insurance against the handler itself being disconnected
		while the list remains unchanged.
	*/
	class _U2Share RequestHandlerHolder : public UtilityAlloc
	{
	protected:
		U2_RW_MUTEX(mRWMutex);
		RequestHandler* mHandler;
	public:
		RequestHandlerHolder(RequestHandler* handler)
			: mHandler(handler)	{}

		// Disconnect the handler to allow it to be destroyed
		void disconnectHandler()
		{
			// write lock - must wait for all requests to finish
			U2_LOCK_RW_MUTEX_WRITE(mRWMutex);
			mHandler = 0;
		}

		/** Get handler pointer - note, only use this for == comparison or similar,
			do not attempt to call it as it is not thread safe. 
		*/
		RequestHandler* getHandler() { return mHandler; }

		/** Process a request if possible.
		@return Valid response if processed, null otherwise
		*/
		Response* handleRequest(const Request* req, const WorkQueue* srcQ)
		{
			// Read mutex so that multiple requests can be processed by the
			// same handler in parallel if required
			U2_LOCK_RW_MUTEX_READ(mRWMutex);
			Response* response = 0;
			if (mHandler)
			{
				if (mHandler->canHandleRequest(req, srcQ))
				{
					response = mHandler->handleRequest(req, srcQ);
				}
			}
			return response;
		}

	};
	// Hold these by shared pointer so they can be copied keeping same instance
	typedef U2SharedPtr<RequestHandlerHolder>       RequestHandlerHolderPtr;

    typedef std::list<RequestHandlerHolderPtr>      RequestHandlerList;
	typedef std::list<ResponseHandler*>             ResponseHandlerList;
	typedef std::map<u2uint16, RequestHandlerList>  RequestHandlerListByChannel;
	typedef std::map<u2uint16, ResponseHandlerList> ResponseHandlerListByChannel;

	RequestHandlerListByChannel mRequestHandlers;
	ResponseHandlerListByChannel mResponseHandlers;
	RequestID mRequestCount;
	bool mPaused;
	bool mAcceptRequests;
	bool mShuttingDown;

	U2_MUTEX(mRequestMutex)
	U2_MUTEX(mProcessMutex)
	U2_MUTEX(mResponseMutex)
	U2_RW_MUTEX(mRequestHandlerMutex);


	void processRequestResponse(Request* r, bool synchronous);
	Response* processRequest(Request* r);
	void processResponse(Response* r);
	/// Notify workers about a new request. 
	virtual void notifyWorkers() = 0;
	/// Put a Request on the queue with a specific RequestID.
	void addRequestWithRID(RequestID rid, u2uint16 channel, u2uint16 requestType, const Any& rData, u2uint8 retryCount);

};



U2EG_NAMESPACE_END


#endif

